package com.ctshk.rpc.order.custom.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;

public class OrderUtil {

    public static final String yyMMdd = "yyMMdd";
    public static final String HHmmss = "HHmmss";
    public static final String HH_mm_ss = "HH:mm:ss";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyMMddHHmmss = "yyMMddHHmmss";

    /**
     * 获取随机数2
     */
    public static String getRandom2() {
        Random random = new Random();
        int ends = random.nextInt(99);
        return String.format("%02d", ends);
    }

    /**
     * 获取随机数6
     */
    public static String getRandom6() {
        Random random = new Random();
        int ends = random.nextInt(999999);
        return String.format("%06d", ends);
    }

    /**
     * 获取随机数6
     */
    public static String getRandom3() {
        Random random = new Random();
        int ends = random.nextInt(999);
        return String.format("%03d", ends);
    }

    /**
     * 获取订单号
     */
    public static String getOrderNumber() {
        //TODO 部门+销售人等
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(yyMMdd)) + getRandom3();
    }

    /**
     * 获取退团单号
     */
    public static String getQuitNumber() {
        //TODO 不对
        LocalDateTime now = LocalDateTime.now();
        return "TKD" + now.format(DateTimeFormatter.ofPattern(yyMMdd)) + getRandom2();
    }

    /**
     * 获取星期几
     */
    public static String getWeekOfDate(LocalDateTime now) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        return weekDays[now.getDayOfWeek().getValue() - 1];
    }

    /**
     * 获取日期和星期几
     */
    public static String getDayAndWeek(LocalDateTime now) {
        return now.format(DateTimeFormatter.ofPattern(yyyyMMdd)) + " " + OrderUtil.getWeekOfDate(now);
    }

    /**
     * 获取收据订单号（销售）
     */
    public static String getReceiptNumberA() {
        //TODO 門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（S）
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(yyMMdd)) + "A" + now.format(DateTimeFormatter.ofPattern(HHmmss)) + getRandom6() + "（S）";
    }

    /**
     * 获取收据订单号（保险）
     */
    public static String getReceiptNumberB() {
        //TODO 門市編號+員工編號+年後2位+月日4位+B+時分秒6位+（S）
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(yyMMdd)) + "B" + now.format(DateTimeFormatter.ofPattern(HHmmss)) + getRandom6() + "（S）";
    }

    /**
     * 获取收据单号（通知书）
     */
    public static String getReceiptNumberQuit() {
        //TODO 門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（T）
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(yyMMdd)) + "A" + now.format(DateTimeFormatter.ofPattern(HHmmss)) + getRandom6() + "（T）";
    }

    /**
     * 获取收款单号
     */
    public static String getCollectionNumber() {
        LocalDateTime now = LocalDateTime.now();
        return "SKD" + now.format(DateTimeFormatter.ofPattern(yyMMddHHmmss)) + getRandom2();
    }

    /**
     * 获取退款单号
     */
    public static String getRefundNumber() {
        LocalDateTime now = LocalDateTime.now();
        return "TKD" + now.format(DateTimeFormatter.ofPattern(yyMMddHHmmss)) + getRandom2();//如果不足两位，前面补0;
    }


    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

}
